// miniprogram/pages/TUI-Group/memberprofile-group/memberprofile.js
import logger from '../../../utils/logger';
// eslint-disable-next-line no-undef
Page({

  /**
   * 页面的初始数据
   */
  data: {
    personalProfile: {},
    name: '',
    show: false,
    groupID: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    logger.log(`| TUI-Group | onLoad | personalProfile: ${JSON.parse(options.personalProfile)}`);
    this.setData({
      personalProfile: JSON.parse(options.personalProfile),
      groupID: options.groupID
    });
  },
  check_name() {
    this.setData({
      show: true
    })
  },
  change_show() {
    this.setData({
      show: false
    })
  },
  change_name(e) {
    this.setData({
      name: e.detail.value
    })
  },
  change_ok() {
    if (!this.data.name) {
      getApp().showToast('请输入群名片')
      return
    }
    getApp().globalData.posts('/index.php/api/ouyoung/chat/sorting/ouyapi/check_name', {
      group_id: this.data.groupID,
      id: this.data.personalProfile.userID,
      name: this.data.name
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        let promise = wx.$TUIKit.getGroupMemberProfile({
          groupID: this.data.groupID,
          userIDList: [this.data.personalProfile.userID],
        });
        promise.then((imResponse) => {
          this.setData({
            personalProfile: imResponse.data.memberList[0]
          })
        }).catch(function (imError) {
          console.warn('getGroupMemberProfile error:', imError);
        });
        this.change_show()
      })
    })
  },
});